/**
 * Created by lzz on 2017/6/5.
 */
import {Injectable} from '@angular/core';
import {Http, URLSearchParams} from '@angular/http';
import {Book} from '../module/book';
import {Comment} from '../module/comment';
import 'rxjs/add/operator/toPromise';
import {ReplyUser} from '../module/replyUser';
import {Email} from '../module/email';
import {RepiyPushLog} from '../module/repiyPushLog';


const BASE_URL = 'http://192.168.15.170:7000';
// const BASE_URL = 'http://127.0.0.1:9100';
const BOOK_LIST = BASE_URL + '/api/book/getLastBookList/';
const HOTBOOK_LIST = BASE_URL + '/api/book/getHotBookList/';
const GET_BOOK = BASE_URL + '/api/book/getBook/';
const PUSH_BOOK = BASE_URL + '/api/book/pushBook/';
const GET_BOOK_COMMENTS = BASE_URL + '/api/bookComment/getCommentList/';
const SEARCH_BOOK = BASE_URL + '/api/book/searchBook/';
const POST_WISH = BASE_URL + '/api/wish/postWish/';
const POST_COMMENT = BASE_URL + '/api/bookComment/postComment/';
const USER_LOGIN = BASE_URL + '/api/user/login/';
const GET_EMAILS = BASE_URL + '/api/email/getEmails/';
const POST_USER = BASE_URL + '/api/user/postUser/';
const POST_EMAIL = BASE_URL + '/api/email/postEmail/';
const DELETE_EMAIL = BASE_URL + '/api/email/deleteEmail/';
const UPDATE_USER = BASE_URL + '/api/user/updateUser/';

const GET_PUSHLOGS = BASE_URL + '/api/log/getPushLog/';


@Injectable()
export class HttpService {
  constructor(private http: Http) {
  }

  /**
   * 获取最新图书列表
   * @returns {Promise<any|T>}
   */
  getLastBookList(): Promise<Book[]> {
    return this.http.get(BOOK_LIST)
      .toPromise()
      .then(response => response.json().data as Book[])
      .catch(this.handleError);
  }

  /**
   * 获取最热图书列表
   * @returns {Promise<any|T>}
   */
  getHotBookList(): Promise<Book[]> {
    return this.http.get(HOTBOOK_LIST)
      .toPromise()
      .then(response => response.json().data as Book[])
      .catch(this.handleError);
  }

  /**
   * 用户注册
   * @returns {Promise<any|T>}
   */
  postUser(email: String, password: String): Promise<ReplyUser> {
    const params = new URLSearchParams();
    params.set('email', '' + email);
    params.set('password', '' + password);
    return this.http.get(POST_USER, {search: params})
      .toPromise()
      .then(response => response.json())
      .catch(this.handleError);
  }

  /**
   * 获取图书详情
   * @param id
   * @returns {Promise<any|T>}
   */
  getBook(id: number): Promise<Book> {
    const params = new URLSearchParams();
    params.set('id', '' + id);
    return this.http.get(GET_BOOK, {search: params})
      .toPromise()
      .then(response => response.json().data as Book)
      .catch(this.handleError);
  }

  /**
   * 图书推送
   * @param bookId
   * @param token
   * @param mail
   * @returns {Promise<any|T>}
   */
  pushBook(bookId: number, token: string, mail: string): Promise<string> {
    const params = new URLSearchParams();
    params.set('id', '' + bookId);
    params.set('userToken', token);
    params.set('mail', mail);
    return this.http.get(PUSH_BOOK, {search: params})
      .toPromise()
      .then(response => response.json().data as string)
      .catch(this.handleError);
  }

  /**
   * 图书查找
   * @param key
   * @returns {Promise<any|T>}
   */
  searchBook(key: string): Promise<Book[]> {
    const params = new URLSearchParams();
    params.set('name', '' + key);
    return this.http.get(SEARCH_BOOK, {search: params})
      .toPromise()
      .then(response => response.json().data as Book[])
      .catch(this.handleError);
  }

  /**
   * 获取评论列表
   * @param bookId
   * @param endId
   * @returns {Promise<any|T>}
   */
  getCommentList(bookId: number, endId: number): Promise<Comment[]> {
    const params = new URLSearchParams();
    params.set('bookId', '' + bookId);
    params.set('endID', '' + endId);
    return this.http.get(GET_BOOK_COMMENTS, {search: params})
      .toPromise()
      .then(response => response.json().data as Comment[])
      .catch(this.handleError);
  }

  /**
   * 发布心愿单
   * @param userId
   * @param bookName
   * @param author
   * @returns {Promise<any|T>}
   */
  postWish(userId: 1, bookName: string, author: string): Promise<string[]> {
    const params = new URLSearchParams();
    params.set('userId', '' + 1);
    params.set('bookName', '' + bookName);
    params.set('author', '' + author);
    return this.http.get(POST_WISH, {search: params})
      .toPromise()
      .then(response => response.json() as string[])
      .catch(this.handleError);
  }

  /**
   * 发布留言
   * @param token
   * @param bookId
   * @param content
   * @returns {Promise<any|T>}
   */
  postComment(token: string, bookId: number, content: string): Promise<string> {
    const params = new URLSearchParams();
    params.set('userToken', token);
    params.set('bookId', '' + bookId);
    params.set('content', content);
    return this.http.get(POST_COMMENT, {search: params})
      .toPromise()
      .then(response => response.json().data as string)
      .catch(this.handleError);
  }


  /**
   * 用户登录
   * @param email
   * @param pwd
   * @returns {Promise<any|T>}
   */
  userLogin(email: string, pwd: string): Promise<ReplyUser> {
    const params = new URLSearchParams();
    params.set('email', email);
    params.set('password', pwd);
    return this.http.get(USER_LOGIN, {search: params})
      .toPromise()
      .then(response => response.json() as ReplyUser)
      .catch(this.handleError);
  }


  /**
   * 获取用户邮箱列表
   * @param token
   * @returns {Promise<any|T>}
   */
  getEmails(token: string): Promise<Email[]> {
    const params = new URLSearchParams();
    params.set('userToken', token);
    return this.http.get(GET_EMAILS, {search: params})
      .toPromise()
      .then(response => response.json().data as Email[])
      .catch(this.handleError);
  }

  /**
   * 删除邮箱
   * @param token
   * @param mailId
   * @returns {Promise<any|T>}
   */
  deleteEmail(token: string, mailId: number): Promise<string> {
    const params = new URLSearchParams();
    params.set('userToken', token);
    params.set('id', '' + mailId);
    return this.http.get(DELETE_EMAIL, {search: params})
      .toPromise()
      .then(response => response.json().msg as string)
      .catch(this.handleError);
  }

  /**
   * 添加邮箱
   * @param token
   * @param mail
   * @returns {Promise<any|T>}
   */
  postEmail(token: string, mail: string): Promise<string> {
    const params = new URLSearchParams();
    params.set('userToken', token);
    params.set('mail', mail);
    return this.http.get(POST_EMAIL, {search: params})
      .toPromise()
      .then(response => response.json().msg as string)
      .catch(this.handleError);
  }

  /**
   *获取推送日志
   */
  getPushlog(token: string, endId: number): Promise<RepiyPushLog> {
    const params = new URLSearchParams();
    params.set('userToken', token);
    params.set('endId', '' + endId);
    return this.http.get(GET_PUSHLOGS, {search: params})
      .toPromise()
      .then(response => response.json() as RepiyPushLog)
      .catch(this.handleError);
  }

  /**
   * 异常处理
   * @param error
   * @returns {Promise<never>}
   */
  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }

  /**
   *修改个人介绍
   */
  postModification(email: string, nickname: string, password: string, userToken: string, headimgurl: string, sex: string): Promise<ReplyUser> {
    const params = new URLSearchParams();
    params.set('email', email);
    params.set('nickname', nickname);
    params.set('password', password);
    params.set('headimgurl', headimgurl);
    params.set('sex', sex);
    params.set('userToken', userToken);
    return this.http.get(UPDATE_USER, {search: params})
      .toPromise()
      .then(response => response.json() as ReplyUser)
      .catch(this.handleError);
  }

}


